package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.RoleMapper;
import com.zcoj.mapper.UserRoleMapper;
import com.zcoj.pojo.Dto.Role;
import com.zcoj.pojo.Dto.UserRole;
import com.zcoj.service.RoleService;
import com.zcoj.util.RedisCaches;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/02/24 16:16
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RedisCaches redisCaches;

    @Override
    public Integer create(Role role) {
        roleMapper.insert(role);
        return role.getId();
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }



    @Override
    public void updateById(Role role) {
        roleMapper.updateById(role);
    }



    @Override
    public Role findById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public Role findOne(Role role) {
        Wrapper<Role> wrapper = new QueryWrapper<>(role);
        return roleMapper.selectOne(wrapper);
    }

    @Override
    public List<Role> findList(Role role) {
        Wrapper<Role> wrapper = new QueryWrapper<>(role);
        return roleMapper.selectList(wrapper);
    }

    @Override
    public List<Role> findListDesc(Role role) {
        Role one = findOne(role);
        if( Objects.nonNull(one)){
            QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
            queryWrapper.ge("id" ,one.getId());
            return roleMapper.selectList(queryWrapper);
        }
        return null;
    }

    @Override
    public List<String> findByUserId(Integer id) {
        QueryWrapper<UserRole> queryWrapperUR=new QueryWrapper<>();
        queryWrapperUR.eq("uid",id);
        List<UserRole> userRoleList= userRoleMapper.selectList(queryWrapperUR);


        List<String> listStrings=new ArrayList<>();
        for (int i = 0; i < userRoleList.size(); i++) {
             listStrings.add(roleMapper.selectById(userRoleList.get(i).getRid()).getRole());
        }
        return listStrings;
    }

    @Override
    public PageData<Role> findPage(PageWrap<Role> pageWrap) {
        IPage<Role> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(roleMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(Role role) {
        Wrapper<Role> wrapper = new QueryWrapper<>(role);
        return roleMapper.selectCount(wrapper);
    }
}
